package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.common.DeleteException;
import com.itheima.entity.Category;
import com.itheima.entity.Dish;
import com.itheima.entity.Setmeal;
import com.itheima.mapper.CategoryMapper;
import com.itheima.service.CategoryService;
import com.itheima.service.DishService;
import com.itheima.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
//继承service实现类,帮忙重写BaseMapper<>里面的方法
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Autowired
    DishService dishService;
    @Autowired
    SetmealService setmealService;
    public void remove(Long id){
        LambdaQueryWrapper<Dish> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(Dish ::getCategoryId,id);
        int count1 = dishService.count(queryWrapper1);
        if (count1 > 0) {
            throw new DeleteException("当前分类下关联了其他菜品，无法删除");
        }

        LambdaQueryWrapper<Setmeal> queryWrapper2 = new LambdaQueryWrapper<>();
        queryWrapper2.eq(Setmeal ::getCategoryId,id);
        int count2 = setmealService.count(queryWrapper2);
        if (count2 > 0) {
            throw new DeleteException("当前分类下关联了其他菜品，无法删除");
        }
        this.removeById(id);
    }
}
